#include <stdio.h>
#include<stdlib.h>
int *a;
int n;
/*to find the maximum number in the list */
void max(int n ,int a[])
{
 int max1,i;
 max1=a[0];
 for (i=1;i<n;i++)
  {
    if(a[i]>max1)
    max1=a[i];
  }
printf("%d",max1);
}
/*to find the minimum number*/
void min(int n ,int a[])
{
 int min_no,i;
 min_no=a[0];
 for (i=1;i<n;i++)
 {
   if(a[i]<min_no)
   min_no=a[i];
  }
 printf("the minimum number is %d",min_no);
}
/*reverse of the list given*/
void reverse(int n ,int a[])
{
int i,j,k,temp;
for(i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(k=0;k<n;k++)
 {
   printf("\n %d",a[k]);
  }
}
/*to find the index of the element of the element given*/
void index1(int n,int a[])
{
int i, x;
printf("enter the element you want to find the index of");
scanf("%d",&x);
for(i=0;i<n;i++)
 { 
  if(x==a[i])
    {printf("the first occurence of %d is at %d position",x,i);
    break;}
  
  }
}
/*to count the number of elements in the list*/
void count(int n,int a[])
{
int x,i,c=0;
printf("enter the element you want to count the number of ocurences for");
scanf("%d",&x);
for(i=0;i<n;i++)
{ 
  if(x==a[i])
    {c++;}
}
  printf("%d",c);
}  
/*to sort the given list*/
int sort(int n,int a[])
{
 int i,temp,j;
 for(i=0;i<n;i++)
 {for(j=i;j<n;j++)
  {if(a[j]<a[i])
   {temp=a[i];
    a[i]=a[j];
    a[j]=temp;}
  }
  }
for(i=0;i<n;i++)
 {printf("%d ",a[i]);}
}
/*to delete the element*/
 void delete(int n,int a[])
{int x, i,j,k;
printf("enter the element you want to delete");
scanf("%d",&x);
for (i=0;i<n;i++)
{ 
 if(a[i]==x)
  {
      for (j=i;j<n-1;j++)
       {a[j]=a[j+1];
         }

}

}n--;

} 
/*display function*/
void display(int n, int a[])
{
int i;
for(i=0;i<n;i++)
{printf("%d",a[i]);}
}
/*append the given element in the list*/
void append(int n, int *a)                                      
{

int i,x;
 n=n+1;
printf("enter the element you want to append in the list\n");
scanf("%d",&x) ;
 a = (int *) realloc((int *)a,n*sizeof(int)); 

 a[n-1]=x;
for(i=0;i<n;i++)
{ printf("%d",a[i]);}

} 
/* add the new list to the main list*/                                                  
int *extend(int n , int *a,int m)
{int  k ,i,j;

int *b;
b=(int *)malloc(m*sizeof(int));                     
int n1=n;
 n=n+m;

 printf("eneter the elements of the list\n");
 for(k=0;k<m;k++)
{ scanf("%d",&b[k]);
}
a = (int *) realloc((int *)a,n*sizeof(int)); 
for(i=n1,j=0;i<n,j<m;i++,j++)
{ a[i]=b[j];
}
for(i=0;i<n;i++)
printf("%d", a[i]);
printf("\n");
return a;
}/*insert the element at ith position*/
void insert(int n, int *a)
{int i,j,k,x,y;                                                                    
n=n+1;
printf("enter the element you want to insert in the array");
scanf("%d",&x);
 a = (int *) realloc((int *)a,n*sizeof(int)); 
printf(" enter the position at which you want to eneter the element");
scanf("%d",&y);
for(i=0;i<n;i++)
{
if(i==y){
for(j=n-1;j>=i;j--)
{
a[j]=a[j-1];                                                 
 }
a[i]=x;    
}}
for(i=0;i<n;i++)
printf("%d", a[i]);}
/*delete the element at position and print the deleted elemennt*/
void pop(int n, int a[])
{
int x,i;
printf("enter the position at which you want to delete\n");
scanf("%d",&x);
printf("%d",a[x]);
 for (i=x;i<n-1;i++)
{
 a[i]=a[i+1];
}n--;

}

void  main(int n, int *a)
{
int i,ch,x,m,ans;
printf("enter the no. of elements you want in the array");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
for (i=0;i<n;i++)
  {
  	printf("enter the elements");
    	scanf("%d",&a[i]);
  }

do{
printf("**Enter the choice**\n");
printf("1:maximum number in the list\n");
printf("2:minimum number in the list\n");
printf("3:reverse of the array\n");
printf("4:index of the number\n");
printf("5:count the number of occurence\n");
printf("6:sort the list\n");
printf("7:delete an element from the array\n");
printf("8:append the list \n");
printf("9:extend the list\n");
printf("10:insert the element\n");
printf("11:pop the element\n");


scanf("%d",&ch);
switch(ch)
{ 
  case 1:max(n,a);
         break;
  case 2:min(n,a);
         break;
  case 3:reverse(n,a);
         break;
  case 4:index1(n,a);
         break;
  case 5:count(n,a);
         break;
  case 6:sort(n,a);
         break;
  case 7: delete(n,a);
              n--;
             printf("\n");
              display(n,a);
          break;
   case 8: append(n,a);
                printf("\n");
	         n=n+1;
                  display(n,a);
               break;
   case 9:printf("\n");
           printf("enter the size of new list\n");
          scanf("%d",&m);
          a=extend(n,a,m);  
             n=n+m;
              break;
   case 10: insert(n,a);
                 printf("\n");
	         n=n+1;
                 display(n,a);
               break;
    case 11: pop(n,a);
                  printf("\n");
                  display(n-1,a);
                       break;
  default :printf("wrong choice");
}
printf("\n");
printf("do you want to continue\n");
printf("Enter 0 for no and 1 for yes\n");
scanf("%d",&ans);
}
while(ans ==1);
}
